/*
 * NetUtils.hpp
 *
 *  Created on: 23.05.2011
 *      Author: mifodix
 */

#ifndef NETUTILS_HPP_
#define NETUTILS_HPP_

#ifndef __cplusplus
#error Need C++-compile to process this file
#endif

#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
	#include "winsock2.h"
	#pragma comment (lib, "ws2_32.lib")
	#include <stdint.h>
	#include "Ws2tcpip.h"
	#define CLOSE_SOCKET(x) closesocket(x)
	#define SOCK_OPTION_TYPE char
	#define FINISH() WSACleanup();
	#define INITIALIZE() WSADATA wsaData; \
									int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); \

#endif

#if  defined(linux) || defined(__linux) || defined(unix) || defined(__unix)
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <sys/types.h>
	#include <arpa/inet.h>
	#include <unistd.h>
	#define INVALID_SOCKET -1
	#define SOCKET_ERROR -1
	#define CLOSE_SOCKET(x) close(x)
	#define SOCKET int
	#define SOCK_OPTION_TYPE int
	#define INITIALIZE()
	#define FINISH()
#endif

#include <stdint.h>
#include <list>
#include "string.h"
/*
 *
 */
class NetUtils
{
public:
	NetUtils();
	static uint32_t String2IP(const char *strIP);
	static char* IP2String(uint32_t networkOrderedIP);
	static uint32_t NetOrd2HostOrd(uint32_t networkOrder);
	static uint16_t NetOrd2HostOrd(uint16_t networkOrder);
	static uint32_t HostOrd2NetOrd(uint32_t hostOrder);
	static uint16_t HostOrd2NetOrd(uint16_t hostOrder);
	static size_t List2Chars(std::list<uint32_t> l, char *&array, bool toNetOrder=false);
	static bool CheckIPSubnet(uint32_t ip, uint32_t subnet, uint32_t mask);
	virtual ~NetUtils();
};

#endif /* NETUTILS_HPP_ */
